import sys
import subprocess

try:
    from openpyxl import Workbook
except ImportError:
    print("检测到未安装openpyxl库，正在尝试自动安装...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "openpyxl"])
        from openpyxl import Workbook
        print("openpyxl库安装成功！")
    except Exception as e:
        print(f"自动安装失败: {e}")
        print("请手动运行以下命令安装：")
        print("pip install openpyxl")
        exit(1)

# 创建一个新的工作簿
wb = Workbook()

# 获取活动工作表
ws = wb.active

# 设置工作表标题
ws.title = "示例数据"

# 写入表头
ws['A1'] = "姓名"
ws['B1'] = "年龄"
ws['C1'] = "城市"

# 写入示例数据
data = [
    ("张三", 25, "北京"),
    ("李四", 30, "上海"),
    ("王五", 28, "广州")
]

for row in data:
    ws.append(row)

# 保存Excel文件
wb.save("output.xlsx")

print("Excel文件已创建: output.xlsx")


